<%--
  Created by IntelliJ IDEA.
  User: LuoQing
  Date: 2022/1/12
  Time: 10:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>给角色设置菜单</title>
</head>
<body>
<table>
    <tr>
        <td>
            <c:forEach items="${menus}" var="caidan">
                <input type="checkbox" name="aa" class="tuo_${caidan.key.id}" value="${caidan.key.id}"  onclick="check(${caidan.key.id})">${caidan.key.name}
                <ul style="list-style: none;">
                    <c:forEach items="${caidan.value}" var="xiaji">
                        <li>
                            <input type="checkbox" name="aa" class="tuo_${xiaji.key.id} xiaji_${xiaji.key.parentId}" onclick="xiacheck(${xiaji.key.parentId})" value="${xiaji.key.id}">${xiaji.key.name}
                            <ul style="list-style: none;">
                                <c:forEach items="${xiaji.value}" var="xiaji1">
                                    <li>
                                        <input type="checkbox" name="aa" class="xiaji_${xiaji1.parentId}" onclick="xiacheck(${xiaji1.parentId})" value="${xiaji1.id}">${xiaji1.name}
                                    </li>
                                </c:forEach>
                            </ul>
                        </li>
                    </c:forEach>
                </ul>
            </c:forEach>
        </td>
        <td style="position: absolute;top: 35px">
            <select id="jues">
            </select>
            <button onclick="xz()">提交</button>
            <button onclick="fh()">返回</button>
        </td>
    </tr>
</table>
<script src="/js/jquery-3.3.1.js"></script>
<script>
    queryAllRoles()
    function queryAllRoles() {
        $.getJSON("/suc/selectAllRoles",function (data) {
            $("#jues").empty()
            $("#jues").append("<option value=''>"+'---请选择角色---'+"</option>")
            for(let i=0;i<data.length;i++){
                $("#jues").append(" <option value="+data[i].rid+">"+data[i].descn+"</option>")
            }
        })
    }
    function xz() {
        var cdname=$('input:checkbox[name="aa"]:checked').map(function () {
            return $(this).val();
        }).get().join(",");
        var cd=$("#jues").val();
        if(cd){
            if(cdname){
               $.getJSON("/suc/AddMenu",{user_id:cd,menu_id:cdname},function (data) {
                    if(data==1){
                        alert("添加菜单成功")
                    }else{
                        alert("添加失败")
                    }
               })
            }else {
                alert("设置未选择")
            }
        }else{
            alert("角色未选择")
        }
    }
    function check(sum) {
        var fu = document.getElementsByClassName("tuo_"+sum)[0].checked;
        var zi = document.getElementsByClassName("xiaji_"+sum);
        for (var i=0;i<zi.length;i++){
            zi[i].checked=fu;
        }
    }
    function xiacheck(sum){
        var xx=false;
        var fu = document.getElementsByClassName("tuo_"+sum)[0];
        var zi = document.getElementsByClassName("xiaji_"+sum);
        for (var i=0;i<zi.length;i++){
            if(zi[i].checked){
                xx=true;
                break;
            }else{
                xx=false;
            }
        }
        fu.checked=xx;
    }
    $("input :checkbox").click(function(){
        var a=$("ul :checkbox").size()
        var b=$("ul :checkbox:checked").size()
        if(a==b&&b!=0){
            $(":checkbox:eq(0)").prop("checked","true");
        }else{
            $(":checkbox:eq(0)").prop("checked","");
        }
    })
    function fh() {
        location.href="/lqym/quan.jsp";
    }
</script>
</body>
</html>
